$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		// save();
	}
});
function save() {
	let getUserIntegral = [], integralUser = [];

	//商品规格
	$(".shopBox").find('.shopArg').each(function (index,val){
		// getUserIntegral.push({'title': $(val).find('.shopModel').val(),'val': $(val).find('.shopSize').val()})
		getUserIntegral.push($(val).find('.shopModel').val(),$(val).find('.shopSize').val())
	})

	// $("#specificationName").val( JSON.stringify(getUserIntegral) )
	$("#specificationName").val( getUserIntegral.join(',') )

	//商品参数
	$(".shopNew").find('.shopArg').each(function (index,val){
		integralUser.push({'title': $(val).find('.shopArguments').val(),'val': $(val).find('.shopVal').val()})
		// integralUser.push( $(val).find('.shopArguments').val(),$(val).find('.shopVal').val())
	})
	$("#specification").val( JSON.stringify(integralUser) )

	// $("#specification").val( integralUser.join(',') )

	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/integralStoreCommodity/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}



//上传品牌照片
	var arrImg = [];
	layui.use('upload', function () {
		var upload = layui.upload;
		upload.render({
			elem: '#btnAdd',
			url: '/common/sysFile/upload',
			accept: 'images', //只允许上传图片
			acceptMime: 'image/*', //只筛选图片
			multiple:true,
			number:5,
			size:10240,
			done: function (r) {
				var str = '';
				arrImg.push(r.data);
				str = arrImg.join();
				$("#brandImg").val(str);
				str = '';
				arrImg.forEach(function (item,index){
					str += `<div class="closeBtn"><img class="upImg" src="${item}" /><span class="iconclose"><img src="../../img/close.png"></span></div>`
				})
				$("#brandImgs").html(str)

				$(".iconclose").click(function (){
					let deleImg = $(this).parent('.closeBtn').find('img').attr('src')
					arrImg.forEach(function (v,k){
						if ( deleImg == v ){
							arrImg.splice(k,1)
						}
					})
					$(this).parent('.closeBtn').remove();
				})

			},
			error: function (r) {
				layer.msg(r.msg);
			}
		});
	});

	var listImg = [];
	layui.use('upload', function () {
		var upload = layui.upload;
		upload.render({
			elem: '#btnAdd1',
			url: '/common/sysFile/upload',
			accept: 'images', //只允许上传图片
			acceptMime: 'image/*', //只筛选图片
			multiple:true,
			number:5,
			size:10240,
			done: function (r) {
				var str = '';
				listImg.push(r.data);
				str = listImg.join();
				$("#brandImg1").val(str);
				str = '';
				listImg.forEach(function (item,index){
					str += `<div class="closeBtn"><img class="upImg" src="${item}" /><span class="iconclose"><img src="../../img/close.png"></span></div>`
				})
				$("#brandImgs1").html(str)

				$(".iconclose").click(function (){
					let deleImg = $(this).parent('.closeBtn').find('img').attr('src')
					listImg.forEach(function (v,k){
						if ( deleImg == v ){
							listImg.splice(k,1)
						}
					})
					$(this).parent('.closeBtn').remove();
				})
			},
			error: function (r) {
				layer.msg(r.msg);
			}
		});
	})

//商品参数
$(".changeArg").click(function (){
	let str = `
		<div class="shopArg">
			<input type="text" class="shopArguments">
			<input type="text" class="shopVal">
			<div class="clickInfinite">
				<p class="changeArgDele changeAed">删除</p>
			</div>
		</div>
	`;
	$(".shopNew").append(str);
})

//商品参数删除
$(document).on('click','.changeAed',function (){
	$(".changeAed").each(function (index){
		$(this).attr('data-index',index);
	})
	$(".shopNew").find('.shopArg').eq(parseInt( $(this).attr('data-index') )+ 1).remove();
})

//商品规格
$(".changeAdd").click(function (){
	let str = `
		<div class="shopArg">
			<input type="text" class="shopModel">
			<input type="text" class="shopSize">
			<div class="clickInfinite">
				<p class="changeAddDele changeDle">删除</p>
			</div>
		</div>
	`;
	$(".shopBox").append(str);
})

var max = [];
//商品规格删除
$(document).on('click','.changeDle',function (){
	$(".changeDle").each(function (index){
		$(this).attr('data-index',index);
	})
	$(".shopBox").find('.shopArg').eq(parseInt( $(this).attr('data-index') )+ 1).remove();
	max[$(this).parents('.clickInfinite').siblings('.shopSize').attr('index')] = 0;
	$("#inventoryQuantity").val( max.reduce((n,m) => n + m) )
})

sizeInput()
function sizeInput(){
	$(document).on('input','.shopSize',function (val){
		$(".shopSize").each(function (index,val){
			$(val).attr('index',index)
			if( $(val).val() ){
				max[index] = parseInt($(val).val());
			}else{
				max[index] = 0;
			}
		})
		$("#inventoryQuantity").val( max.reduce((n,m) => n + m) )
	})
}
